
/**
 * 实时判断用户是否登录并在会话有效期内的方法，适合需要实时检查的时候用。主要方法为doRequiredLoginAction，其他方法主要是内部使用。
 */
function isLoginedAtServer() {
	$import(["/dwr/interface/loginDwr.js"]);
	var currentUserId;
	loginDwr.getCurrentAppUserId({callback:function (data) {
		currentUserId = data;
	}, async:false});
	return currentUserId == "-2" ? false : true;
}
var globalLoginCallback = null;
function doRequiredLoginAction(loginCallback) {
	globalLoginCallback = loginCallback;
	$importDwr(function () {
		if (!isLoginedAtServer()) {
			if (window.confirm(__FMT.loginConfirm)) {
				fnShowLoginDialog();
			}
		} else {
			execLoginCallback();
		}
	});
}
function execLoginCallback() {
	if (globalLoginCallback!=null) {
		globalLoginCallback.call();
		globalLoginCallback=null;
	}
}
var jLoginDlg=null;
function fnShowLoginDialog() {
	if (jLoginDlg==null) {
		$j(document.body).append('<div id="loginDlg" style="position:absolute;overflow: auto;"></div>');
		if(__isOpenCasService == "1"){
			//CAS
			$j('#loginDlg').append('<iframe width="100%" height="135px" scrolling="no" src="'+__ctxPath+'/customer/loginDlg4Cas.html'+'"></iframe>');
		}else{
			$j('#loginDlg').load(__ctxPath + "/customer/loginDlg.html?decorator=blank",null,function(){
				$("j_username").value = getCurrentUserName(false);
				//弹出层里面focus不生效
			});
		}
		jLoginDlg =$j('#loginDlg'). dialog({ title:__FMT.first_login, modal: true, height:180, width:400, bgiframe: true,
	                            overlay: { opacity: 0.5, background: "black" } });
	}else{
		jLoginDlg.dialog("open");		
	}
}
function fnDoLoginHandler() {
	var username = $("j_username").value;
	var password = $("j_password").value;
	if (trim(username) == "") {
		alert(__FMT.customer_username_isRequired);
		$("j_username").focus();
		return;
	}
	if (trim(password) == "") {
		alert(__FMT.customer_password_isRequired);
		$("j_password").focus();
		return;
	}
	loginDwr._getLoginStatus(username, password, {callback:function (data) {
		if (data == "success") {
			//sysMsg("You signed in system successfully!");
			jLoginDlg.dialog("close");
			fillLoginPrompt();
			execLoginCallback();
		} else {
			$("loginMsg").innerHTML = "<font color='red'>"+__FMT.customer_login_fail+"</font>";
		}
	}});
}
function password_onkeypress(event) {
	var code = (event.keyCode == null) ? event.charCode : event.keyCode;
	if (code == 13) {
		fnDoLoginHandler();
	}
}
//cas登录成功后返回函数
function loginSuccess4Cas(){
	jLoginDlg.dialog("close");
	fillLoginPrompt();
	execLoginCallback();
}

